package la.bbs558;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import la.bbs558.aop.valid.Check;
import la.bbs558.controller.base.BaseController;
import la.bbs558.entity.BbsPost;
import la.bbs558.entity.BbsReply;
import la.bbs558.entity.enums.OssFolder;
import la.bbs558.scheduled.ScheduledManager;
import la.bbs558.service.BbsPostService;
import la.bbs558.service.BbsReplyService;
import la.bbs558.service.StatisticsService;
import la.bbs558.util.OssUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;
import java.util.List;

/**
 * @author: WeiHanmin
 * @description: 我的控制层
 * @time: 2021-07-13 17:13:16
 */
@RestController
@Slf4j
@RequestMapping("/test")
@Check
@SaIgnore
public class TestController extends BaseController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private OssUtil ossUtil;
    @Resource
    private StatisticsService statisticsService;
    @Resource
    private ScheduledManager scheduledManager;

    @Resource
    private BbsPostService bbsPostService;

    @Resource
    private BbsReplyService bbsReplyService;

    @SneakyThrows
    @RequestMapping("testRedirect")
    @SaIgnore
    public void testRedirect() {
        redisTemplate.opsForValue().set("test", "test");
        redisTemplate.expire("test", 10, java.util.concurrent.TimeUnit.SECONDS);
        log.info("testRedirect");
        super.getResponse().sendRedirect("/my/testRedirect2");
    }
    @SneakyThrows
    @RequestMapping("testRedirect2")
    @SaIgnore
    public String testRedirect2() {
        log.info("testRedirect2");
        return DateUtil.now() + redisTemplate.opsForValue().get("test");
    }

    @GetMapping("test")
    @SaIgnore
    public Object test() throws IOException {
        File file = new File("/Users/Shanks/Desktop/iShot_2023-11-10_14.38.07.png");
        return ossUtil.uploadFile(Files.newInputStream(file.toPath()), OssFolder.Ad, "iShot_2023-11-10_14.38.07.png");
    }

    @GetMapping("syncVisitRetention")
    public Object syncVisitRetention() {
        scheduledManager.syncVisitRetention();
        return "success";
    }

    @GetMapping("syncPostReply")
    public String updateTime() {
        List<BbsPost> list = bbsPostService.lambdaQuery()
                .le(BbsPost::getCreateTime, "2023-6-15 00:00:00")
                .list();
        for (BbsPost bbsPost : list) {
            Date newDate = DateUtil.offsetMonth(bbsPost.getCreateTime(), RandomUtil.randomInt(3, 5));
            BbsPost newBbsPost = new BbsPost().setId(bbsPost.getId()).setCreateTime(newDate);
            List<BbsReply> bbsReplyList = bbsReplyService.lambdaQuery()
                    .eq(BbsReply::getPostId, bbsPost.getId())
                    .list();
            for (int i = 0; i < bbsReplyList.size(); i++) {
                BbsReply bbsReply = bbsReplyList.get(i);
                if (i == 0) {
                    new BbsReply().setId(bbsReply.getId()).setCreateTime(newDate).updateById();
                } else {
                    newDate = DateUtil.offsetSecond(newDate, RandomUtil.randomInt(i * 300 * 60, (i + 1) * 300 * 60));
                    new BbsReply().setId(bbsReply.getId()).setCreateTime(newDate).updateById();
                    if (i == bbsReplyList.size() - 1) {
                        newBbsPost.setLastTime(newDate).setLastUser(bbsReply.getCreateUser());
                    }
                }
            }
            newBbsPost.updateById();
        }
        return "success";
    }

}
